package cn.tedu.jsd2205the.back.end.project.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * ip地址工具类
 */
public class IpUtils {
    /**
     * 获取本机IP地址
     */
    public static String getLocalHost() {
        InetAddress inetAddress = null;

        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return inetAddress == null ? null : inetAddress.getHostAddress();
    }

    public static void main(String[] args) throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getLocalHost();

        // 本机IP地址
        System.out.println("Local HostAddress:" + inetAddress.getHostAddress());
        // 本机名称
        System.out.println("Local host name: " + inetAddress.getHostName());
    }

}
